public class CheckPossibility {

    public static void main(String[] args) {
        CheckPossibility checkPossibility = new CheckPossibility();
        int nums[] = {5,7,1,8};
        System.out.println(checkPossibility.checkPossibility(nums));
    }

    public boolean checkPossibility(int[] nums) {
        if (nums.length == 1) return true;
        int index = -1;
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] > nums[i + 1]) {
                if (index == -1) {
                    index = i;
                } else return false;
            }
        }
        if (index == -1) return true;
        int l = index == 0 ? Integer.MIN_VALUE : nums[index - 1];
        int r = index == nums.length - 2 ? Integer.MAX_VALUE : nums[index + 2];
        return nums[index] <= r || nums[index + 1] >= l;
    }
}
